package apps.softtech.Utility;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Bitmap.Config;
import android.graphics.PorterDuff.Mode;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;

public class ImageHelper {
	@SuppressWarnings("deprecation")
	public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, int pixels) {
		Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
				bitmap.getHeight(), Config.ARGB_8888);
		Canvas canvas = new Canvas(output);

		final int color = 0xff424242;
		final Paint paint = new Paint();
		final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
		final RectF rectF = new RectF(rect);
		final float roundPx = bitmap.getWidth();

		paint.setAntiAlias(true);
		canvas.drawARGB(0, 0, 0, 0);
		paint.setColor(color);
		canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
		paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
		canvas.drawBitmap(bitmap, rect, rect, paint);

		return output;
	}

	public static String saveImageToApp(Context context, Bitmap bm, String name) {
		// create a file to write bitmap data
		if (bm == null) {
			Log.e("ImageHelper", "BITMAP IMAGE NULL CANNOT SAVE IMAGE");
			return null;
		}
		String filename = name;
		File f;
		if (filename.contains(".jpg")) {
			f = new File(context.getFilesDir(), filename);
		} else {
			f = new File(context.getFilesDir(), filename + ".jpg");
		}

		// String path = f.getAbsolutePath();

		Log.e("path luu avatar contact", f.getAbsolutePath());
		// write the bytes in file
		try {
			f.createNewFile();
			byte[] bitmapdata = null;
			Bitmap bitmap = bm;
			ByteArrayOutputStream bos = new ByteArrayOutputStream();
			bitmap.compress(CompressFormat.PNG, 0, bos);
			bitmapdata = bos.toByteArray();
			FileOutputStream fos;
			fos = new FileOutputStream(f);
			fos.write(bitmapdata);
			fos.close();
			String path = f.getAbsolutePath();
			Log.e("ImageHelper", "LUU AVATAR THANH CONG");
			return path;
		} catch (FileNotFoundException e) {
			Log.e("ImageHelper", "LOI LUU FILE,FILE NOT FOUND:" + e.toString());
		} catch (IOException e) {
			Log.e("ImageHelper", "LOI LUU FILE:" + e.toString());
		}
		return null;

	}

	public static Bitmap getBitmapFromURL(String linkanh) {
		if (linkanh.trim() == "") {
			return null;
		}
		try {
			Log.e("src", linkanh);
			URL url = new URL(linkanh);
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			conn.setDoInput(true);
			conn.connect();
			InputStream input = conn.getInputStream();
			Bitmap returnBitmap = BitmapFactory.decodeStream(input);
			Log.e("Bitmap", "returned");
			return returnBitmap;
		} catch (IOException e) {
			e.printStackTrace();
			Log.e("Exception", e.getMessage());
			return null;
		}
	}

	public static Bitmap decodeFile(File f, int reqHeight, int reqWidth) {
		try {
			// Decode image size
			BitmapFactory.Options o = new BitmapFactory.Options();
			o.inJustDecodeBounds = true;
			BitmapFactory.decodeStream(new FileInputStream(f), null, o);

			// Find the correct scale value. It should be the power of 2.
			int inSampleSize = 1;
			while (o.outWidth / inSampleSize / 2 >= reqWidth
					&& o.outHeight / inSampleSize / 2 >= reqHeight)
				inSampleSize *= 2;

			// Decode with inSampleSize
			o.inSampleSize = inSampleSize;
			o.inJustDecodeBounds = false;
			return BitmapFactory.decodeStream(new FileInputStream(f), null, o);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}

		return null;
	}

	public static void unbindDrawables(View view) {
		if (view.getBackground() != null) {
			view.getBackground().setCallback(null);
		}
		if (view instanceof ViewGroup) {
			for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
				unbindDrawables(((ViewGroup) view).getChildAt(i));
			}
			((ViewGroup) view).removeAllViews();
		}
	}

	public static String convertInputStreamToString(InputStream inputStream)
			throws IOException {
		BufferedReader bufferedReader = new BufferedReader(
				new InputStreamReader(inputStream));
		String line = "";
		String result = "";
		while ((line = bufferedReader.readLine()) != null)
			result += line;

		inputStream.close();
		return result;

	}
}
